package ercahshu

func buildTree(inorder []int, postorder []int) *TreeNode {
	if len(inorder) == 0 {
		return nil
	}
	top := postorder[len(postorder)-1]
	gen := getMid(inorder, top)
	root := &TreeNode{Val: top}
	root.Left = buildTree(inorder[:gen], postorder[:gen])
	root.Right = buildTree(inorder[gen+1:], postorder[gen:len(postorder)-1])
	return root
}

func getMid(inorder []int, top int) int {
	for i, v := range inorder {
		if v == top {
			return i
		}
	}
	return -1
}
